Page({
  data: {
    dialogVisible: false,
    list: [],
    isFirstLoad: true
  },

  async swipeClose (ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      // vant 对话框
      // this.setData({
      //   dialogVisible: true
      // })

      // 小程序自带对话框
      const res = await wx.showModal({
        content: '是否删除已绑定房屋？',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '删除',
        confirmColor: '#5591af'
      })

      // swiper-cell 滑块关闭
      instance.close()

      if (res.confirm) {
        await wx.http.delete(`/room/${ev.mark.id}`)
        // 前端乐观更新
        this.setData({
          list: this.data.list.filter(v => v.id !== ev.mark.id)
        })
      }
    }
  },

  goDetail (e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + e.mark.id
    })
  },

  addHouse () {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index'
    })
  },

  onShow () {
    this.getList()
  },
  // 获取房屋列表
  async getList () {
    const { data } = await wx.http.get('/room')
    this.setData({ list: data, isFirstLoad: false })
  }
})
